package com.hongxun.design.activitys;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.ProjectsumAdapter;
import com.hongxun.design.bean.ProjectSumBean;
import com.hongxun.design.bean.ProjectSumItemBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.myslider.MyBaseSliderView;
import com.hongxun.design.ui.myslider.MyTextSliderView_Pie;
import com.hongxun.design.ui.statisticsmap.pieview.PieItemBean;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/24.
 * 统计分析
 */
public class StatisticalAnalysisActivity extends BaseActivity implements HeadSecond {

    Handler projectSumHandler, fieldSumHandler;
    ListView projectsum_list;
    ProjectsumAdapter projectsumAdapter;
    List<ProjectSumItemBean> data;
    int wight = 1000;
    SliderLayout sliderLayout;
    TextView title_statis;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statisticalanalysis);
        findBar();
        findView();
    }

    @Override
    public void findBar() {
        getIntentData();
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.codelibrary_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(IconSourse.screen + "", View.VISIBLE, R.color.bar_right, ListenerSet.back, "项目统计", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        headsecond_layout.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentToActivity.intent(mActivity, StatisticsSearchActivity.class);
            }
        });

    }

    private void getIntentData() {

    }

    boolean refflag = false;

    @Override
    protected void onResume() {
        super.onResume();
        if (refflag) {
            RefreshData();
        } else {
            refflag = true;
        }
    }


    @Override
    protected void findView() {
        super.findView();
        data = DataUtil.getProjectSumData(new ProjectSumBean());
        projectsum_list = (ListView) findViewById(R.id.projectsum_list);
        custom_indicator = (PagerIndicator) findViewById(R.id.custom_indicator);
        title_statis= (TextView) findViewById(R.id.title_statis);
        WindowManager wm1 = this.getWindowManager();
        int width1 = wm1.getDefaultDisplay().getWidth();
        wight = (width1 / 360) * 226;
        sliderLayout = (SliderLayout) findViewById(R.id.projectsum_view_slider);
        sliderLayout.setPresetTransformer(SliderLayout.Transformer.Default);//动画
        sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        sliderLayout.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
        sliderLayout.setCustomIndicator((PagerIndicator) findViewById(R.id.custom_indicator));
        sliderLayout.setCustomIndicator(custom_indicator);
        sliderLayout.stopAutoCycle();//关闭自动滑动

        addProjectSumHandler();
        addFieldSumHandler();
        projectSumHandler.sendEmptyMessage(1);
        RefreshData();
    }

    @Override
    public void RefreshData() {
        super.RefreshData();
        ProjectSum();
        getPieItemBean();
    }

    PagerIndicator custom_indicator;
    List[] datas = new List[4];
    public String stage = "";
    public static String startYear = "";
    public static String endYear = "";
    public static String design_unit = "";
    public static String owner_unit = "";
    int statisticsType = 1;//2,3,4

    @Override
    protected void onDestroy() {
        super.onDestroy();
        startYear = "";
        endYear = "";
        design_unit = "";
        owner_unit = "";
        refflag = false;

        sliderLayout.removeAllViews();
    }

    //当数据改动时开始遍历请求下方轮播数据
    private void getPieItemBean() {
        statisticsType = 1;
//        sliderLayout.removeAllSliders();
//        sliderLayout.stopAutoCycle();
        FieldSum();
    }


    /**
     * 加载轮播*
     */

    List<MyTextSliderView_Pie> list = new ArrayList<>();

    private synchronized void addCarouselItem(final List<PieItemBean>[] datas) {

        // sliderLayout.removeAllSliders();

        //list.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (list.size() == 0) {
                    for (int i = 0; i < datas.length; i++) {
                        MyTextSliderView_Pie textSliderView = new MyTextSliderView_Pie(mActivity);
                        textSliderView.setHasTitle(false)
                                .PieData(datas[i])
                                .setOnSliderClickListener(new MyBaseSliderView.OnSliderClickListener() {
                                    @Override
                                    public void onSliderClick(MyBaseSliderView var1) {
                                    }
                                });
                        list.add(textSliderView);

                        //sliderLayout.addSlider(textSliderView);
                    }
                    fieldSumHandler.sendEmptyMessage(1);
                } else {
                    fieldSumHandler.sendEmptyMessage(2);
                }


            }
        }).start();


    }

    /**
     * 项目设计统计阶段*
     */
    private void ProjectSum() {
        MyApp.HTTP.ProjectSum(projectSumHandler, HttpStringUtil.SUCCESSED, startYear, endYear, design_unit, owner_unit);
    }

    /**
     * 设计统计阶段接收器*
     */
    private void addProjectSumHandler() {
        projectSumHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        String s = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            ProjectSumBean bean = FastJsonUtil.JsonToGetProjectSum((String) msg.obj);
                            if (data == null) {
                                data = DataUtil.getProjectSumData(bean);
                            } else {
                                data.clear();
                                data.addAll(DataUtil.getProjectSumData(bean));
                            }
                            projectSumHandler.sendEmptyMessage(1);
                        } else {
                            ToastShow.showShort(mActivity, s);
                        }
                        break;
                    case 1:
                        if (projectsumAdapter == null) {
                            projectsumAdapter = new ProjectsumAdapter(mActivity, data, wight);
                            projectsum_list.setAdapter(projectsumAdapter);
                        } else {
                            projectsumAdapter.update();
                            projectsumAdapter.notifyDataSetChanged();
                        }
                        break;

                    case HttpStringUtil.ERROR:
                        break;
                }
            }
        };
    }


    /**
     * 项目按字段统计*
     */
    private void FieldSum() {
        MyApp.HTTP.FieldSum(fieldSumHandler, HttpStringUtil.SUCCESSED, stage, startYear, endYear, design_unit, owner_unit, statisticsType);
    }


    /**
     * 字段统计接收器*
     */
    private void addFieldSumHandler() {
        fieldSumHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        String s = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            datas[statisticsType - 1] = FastJsonUtil.JsonToGetFieldSum((String) msg.obj, statisticsType);
                            if (statisticsType < 4) {
                                statisticsType++;
                                FieldSum();
                            } else {
                                statisticsType = 1;
                                addCarouselItem(datas);
                            }
                        } else {
                            ToastShow.showShort(mActivity, s);
                        }
                        break;
                    case 1:

                        for (MyTextSliderView_Pie m : list) {
                            sliderLayout.addSlider(m);
                        }

                        sliderLayout.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int i, float v, int i1) {

                            }
                            @Override
                            public void onPageSelected(int i) {
//                                        项目类型统计” 、“项目属性统计” 、“项目进度统计” 、“资金来源统计”
                                switch (i){
                                    case 0:
                                        title_statis.setText("项目类型统计");
                                        break;
                                    case 1:
                                        title_statis.setText("项目属性统计");
                                        break;
                                    case 2:
                                        title_statis.setText("项目进度统计");
                                        break;
                                    case 3:
                                        title_statis.setText("资金来源统计");
                                        break;
                                }
                            }

                            @Override
                            public void onPageScrollStateChanged(int i) {

                            }
                        });
                        // sliderLayout.refreshDrawableState();
                        title_statis.setText("项目类型统计");
                        sliderLayout.setCurrentPosition(0);
                        break;
                    case 2:

                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).PieData(datas[i]);
                            list.get(i).setPiegraphView();
                        }
                        for (int i = 0; i < list.size(); i++) {
                            sliderLayout.moveNextPosition();
                        }
                        //sliderLayout.refreshDrawableState();
                        break;
                    case HttpStringUtil.ERROR:
                        break;
                }
            }
        };
    }


}
